(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.glSystem.updateVerifyController', editController);
	editController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'glSystemService' ];
	function editController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, glSystemService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		ctrl.glSystem = {};
		ctrl.glSystemOld = {};

		glSystemService.initPageCallback(ctrl);
		ctrl.init = function() {
			glSystemService.getGlSystemLayout('updateVerify').then(function(layoutRsponse) {
				if (!layoutRsponse.data) {
					return;
				}
				ctrl.layoutConfig = layoutRsponse.data;
				glSystemService.loadOptions().then(function(glSystemOptions) {
					var glSystem;
					var glSystemOld;
					glSystemService.initOptions(ctrl, glSystemOptions);
					authService.updateVerify(params).then(function(response) {
						glSystem = response.data.model.newModel;
						glSystemOld = response.data.model.oldModel;
						if (glSystem) {
							ctrl.glSystem = glSystem;
							ctrl.glSystemOld = glSystemOld;
				    		ctrl.glSystem.taskId = response.data.task.taskId;
				    	}
					});
				});
			});
		};
		
		ctrl.updateVerifyReject = function() {
			modalService.openPromptDialogs({
				'btnCallback' : {
					'ok' : function(comments) {
						ctrl.glSystem.comments = comments;
						authService.updateVerifyReject(ctrl.glSystem).then(rejectSuccessful, rejectFailed);
					}
				}
			});
		};

		ctrl.updateVerifyApprove = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.approve', args : [ctrl.glSystem.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.updateVerifyApprove(ctrl.glSystem).then(approveSuccessful, approveFailed);
					}
				}
			});
		};

		function approveSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.approve' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.glSystem.list',
					'state' : 'cda.glSystem.list',
					'params' : {}
				}]
			});
		}

		function approveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.reject' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.glSystem.list',
					'state' : 'cda.glSystem.list',
					'params' : {}
				}]
			});
		}

		function rejectFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);